package com.farm.project.controller;

import com.farm.project.domain.ProjectActive;
import com.farm.project.domain.ex.EchartsTaskData;
import com.farm.project.domain.ex.EchartsTaskNode;
import com.farm.project.domain.ex.FlowModel;
import com.farm.project.service.ProjectServiceInter;
import com.farm.project.service.TasklinkServiceInter;
import com.farm.project.service.TasknodeServiceInter;
import com.farm.template.service.TemplateServiceInter;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.Map;

import javax.annotation.Resource;
import org.apache.log4j.Logger;
import javax.servlet.http.HttpSession;
import com.farm.cbtg.service.AppcatalogServiceInter;
import com.farm.configer.service.ConfigerServiceInter;
import com.farm.core.page.ViewMode;
import com.farm.web.WebUtils;

/* *
 *功能：工程项目控制层
 *详细：
 *
 *版本：v0.1
 *作者：FarmCode代码工程
 *日期：20150707114057
 *说明：
 */
@RequestMapping("/proDesigner")
@Controller
public class ProjectDesignerController extends WebUtils {
	private final static Logger log = Logger.getLogger(ProjectDesignerController.class);
	@Resource
	private ProjectServiceInter projectServiceImpl;
	@Resource
	private AppcatalogServiceInter appCatalogServiceImpl;
	@Resource
	private TasknodeServiceInter taskNodeServiceImpl;
	@Resource
	private TasklinkServiceInter taskLinkServiceImpl;
	@Resource
	private ConfigerServiceInter configerServiceImpl;

	@Resource
	private TemplateServiceInter templateServiceImpl;

	@RequestMapping("/list")
	public ModelAndView index(String id, HttpSession session) {
		return ViewMode.getInstance().putAttr("id", id).returnModelAndView("project/TaskFlowDesigner");
	}

	/**
	 * 加载
	 * 
	 * @return
	 */
	@RequestMapping("/loadData")
	@ResponseBody
	public Map<String, Object> loadData(String projectId, HttpSession session) {
		try {
			EchartsTaskData data = projectServiceImpl.getDatas(projectId);
			return ViewMode.getInstance().putAttr("nodes", data.getNodes()).putAttr("links", data.getLinks())
					.returnObjMode();
		} catch (Exception e) {
			log.error(e.getMessage());
			return ViewMode.getInstance().setError(e.getMessage(), e).returnObjMode();
		}
	}

	/**
	 * 加载
	 * 
	 * @return
	 */
	@RequestMapping("/loadActiveData")
	@ResponseBody
	public Map<String, Object> loadActiveData(String activeId, HttpSession session) {
		try {
			ProjectActive active = projectServiceImpl.getActive(activeId);
			EchartsTaskData data = projectServiceImpl.getDatas(active.getProjectid());
			for (EchartsTaskNode node : data.getNodes()) {
				if (node.getName().equals(active.getNodeid())) {
					node.getItemStyle().setBorderColor("#a0c69d");
					node.getItemStyle().setBorderWidth(15);
				}
			}
			return ViewMode.getInstance().putAttr("nodes", data.getNodes()).putAttr("links", data.getLinks())
					.returnObjMode();
		} catch (Exception e) {
			e.printStackTrace();
			log.error(e.getMessage());
			return ViewMode.getInstance().setError(e.getMessage(), e).returnObjMode();
		}
	}

	/**
	 * 添加任务节点
	 * 
	 * @param projectId
	 * @param parentId
	 * @param nodeModel
	 * @param nodeAppId
	 * @param flowModel sequence:顺序，condition：条件
	 * @param session
	 * @return
	 */
	@RequestMapping("/addTaskNode")
	@ResponseBody
	public Map<String, Object> addTaskNode(String projectId, String parentId, String flowModel, HttpSession session) {
		try {
			FlowModel fmodel = FlowModel.valueOf(flowModel);
			projectServiceImpl.addTaskNode(projectId, parentId, fmodel, getCurrentUser(session));
			return ViewMode.getInstance().returnObjMode();
		} catch (Exception e) {
			log.error(e.getMessage());
			return ViewMode.getInstance().setError(e.getMessage(), e).returnObjMode();
		}
	}

	/**
	 * 绑定任务节点到业务
	 * 
	 * @param projectId
	 * @param parentId
	 * @param nodeModel
	 * @param nodeAppId
	 * @param flowModel sequence:顺序，condition：条件
	 * @param session
	 * @return
	 */
	@RequestMapping("/bindTaskNode")
	@ResponseBody
	public Map<String, Object> bindTaskNode(String projectId, String nodeId, String nodeModel, String nodeAppId,
			HttpSession session) {
		try {
			projectServiceImpl.bandTaskNodeToModel(projectId, nodeId, nodeModel, nodeAppId, getCurrentUser(session));
			return ViewMode.getInstance().returnObjMode();
		} catch (Exception e) {
			log.error(e.getMessage());
			return ViewMode.getInstance().setError(e.getMessage(), e).returnObjMode();
		}
	}

	/**
	 * 刪除任务节点
	 * 
	 * @return
	 */
	@RequestMapping("/delTaskNode")
	@ResponseBody
	public Map<String, Object> delTaskNode(String projectId, String nodeId, HttpSession session) {
		try {
			taskNodeServiceImpl.deleteTasknodeEntity(nodeId, getCurrentUser(session));
			return ViewMode.getInstance().returnObjMode();
		} catch (Exception e) {
			log.error(e.getMessage());
			return ViewMode.getInstance().setError(e.getMessage(), e).returnObjMode();
		}
	}

	/**
	 * 刪除任务节点
	 * 
	 * @return
	 */
	@RequestMapping("/delAllNode")
	@ResponseBody
	public Map<String, Object> delAllNode(String projectId, HttpSession session) {
		try {
			projectServiceImpl.deleteAllTasknode(projectId, getCurrentUser(session));
			return ViewMode.getInstance().returnObjMode();
		} catch (Exception e) {
			log.error(e.getMessage());
			return ViewMode.getInstance().setError(e.getMessage(), e).returnObjMode();
		}
	}
}
